package ch.ethz.group3.myalbum.server.utils;

import java.util.ArrayList;
import java.util.Collection;

import ch.ethz.group3.myalbum.shared.CommentModel;
import ch.ethz.group3.myalbum.shared.GeoLocationModel;
import ch.ethz.group3.myalbum.shared.GroupModel;
import ch.ethz.group3.myalbum.shared.ImageModel;

import com.flickr4java.flickr.groups.Group;
import com.flickr4java.flickr.photos.GeoData;
import com.flickr4java.flickr.photos.Photo;
import com.flickr4java.flickr.photos.comments.Comment;

public class Converter {
	public static Collection<GroupModel> groupsModelFromFlickrGroups(
			Collection<Group> fgroups) {
		Collection<GroupModel> groups = new ArrayList<GroupModel>();
		for (Group fgroup : fgroups) {
			GroupModel group = new GroupModel();
			group.setId(fgroup.getId());
			group.setMemberCount(fgroup.getMembers());
			group.setPhotoCount(fgroup.getPhotoCount());
			group.setDescription(fgroup.getDescription());
			group.setGroupName(fgroup.getName());
			group.setThumbnail(fgroup.getBuddyIconUrl());
			groups.add(group);
		}
		return groups;
	}

	public static Collection<ImageModel> imagesModelFromFlickrPhotos(
			Collection<Photo> photos) {
		Collection<ImageModel> images = new ArrayList<ImageModel>();
		for (Photo photo : photos) {
			ImageModel image = new ImageModel();
			image.setId(photo.getId());
			image.setGeoLocation(Converter.geoLocModelFromFlickrGeoData(photo
					.getGeoData()));
			image.setPhotoUrl(photo.getLargeUrl());
			image.setThumbnail(photo.getThumbnailUrl());
			image.setName(photo.getTitle());
			image.setCommentCount(photo.getComments());
			images.add(image);
		}
		return images;
	}

	public static GeoLocationModel geoLocModelFromFlickrGeoData(GeoData geoData) {
		GeoLocationModel geoloc = new GeoLocationModel();
		if (geoData == null) {
			geoloc.setAccuracy(Integer.MIN_VALUE);
		} else {
			geoloc.setAccuracy(geoData.getAccuracy());
			geoloc.setLongitude(geoData.getLongitude());
			geoloc.setLatitude(geoData.getLatitude());
		}
		return geoloc;
	}

	public static Collection<CommentModel> commentsModelFromFlickrComments(
			Collection<Comment> fcomments) {
		Collection<CommentModel> comments = new ArrayList<CommentModel>();
		for (Comment fcomment : fcomments) {
			CommentModel comment = new CommentModel();
			comment.setAuthorId(fcomment.getAuthor());
			comment.setAuthorName(fcomment.getAuthorName());
			comment.setCreationDate(fcomment.getDateCreate());
			comment.setId(fcomment.getId());
			comment.setPermaLink(fcomment.getPermaLink());
			comment.setComment(fcomment.getText());
			comments.add(comment);
		}
		return comments;
	}
}
